var bgDOM = null;

function saveLocation(value) {
	if (value != "") {
		localStorage["tt_location"] = value;
		bgDOM.Init();
	}
};

// Restores select box state to saved value from localStorage.
function restore_options() {
	var location = localStorage["tt_location"];
	if (!location) {
		return;
	}
  
	var select = document.getElementById("location_select");
	for (var i = 0; i < select.children.length; i++) {
		var child = select.children[i];
		if (child.value == location) {
			child.selected = "true";
			break;
		}
	}
};

function FillLocationList() {
	var locations = bgDOM.options.GetLocations();
	
	for (var i = 0; i < locations.length; i++) {
		var newOption = document.createElement("option");
		newOption.text = locations[i].name;
		newOption.value = locations[i].id;
		document.getElementById('location_select').add(newOption, null);
	}
		
	restore_options();
};

function Init() {
	bgDOM = chrome.extension.getBackgroundPage();

	bgDOM.options.GetTrendsAvailable(FillLocationList);
};